package edu.mercer.employmentcalculator;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CurrentCompensation extends Activity
{
	private int mode;
	private EditText txtSalary;
	private EditText txtVacation;
	private EditText txtHolidays;
	private EditText txtSick;
	private EditText txtZip;
	
	private Button nextButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.current_compensation);
		
		this.mode = getIntent().getIntExtra(Constants.Mode, -1);
		
		this.txtSalary = (EditText)findViewById(R.id.txtSalary);
		this.txtVacation = (EditText)findViewById(R.id.txtVacation);
		this.txtHolidays = (EditText)findViewById(R.id.txtHolidays);
		this.txtSick = (EditText)findViewById(R.id.txtSick);
		this.txtZip = (EditText)findViewById(R.id.txtZIP);
		this.nextButton = (Button)findViewById(R.id.btnNext);
		this.nextButton.setOnClickListener(myClickListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	View.OnClickListener myClickListener  =  new View.OnClickListener()
	{		
		public void onClick(View v)
		{
			Intent intent;

			if (mode == Constants.CompareCode)
			{
				intent = new Intent("edu.mercer.POTENTIALCOMPENSATIONCOMPARE");
			}
			else if (mode == Constants.EquivCode)
			{
				intent = new Intent("edu.mercer.POTENTIALCOMPENSATIONEQUIV");
			}
			else
			{
				Toast.makeText(getBaseContext(), "Invalid mode", Toast.LENGTH_SHORT).show();
				return;
			}
			
			intent.putExtras(getCurrentCompensationBundle());
			startActivity(intent);
		}
	};
	
	private Bundle getCurrentCompensationBundle()
	{
		Bundle bundle = new Bundle();
		bundle.putDouble(Constants.CurrentSalary, this.getYearlySalary());
		bundle.putInt(Constants.CurrentVacation, this.getVacationDays());
		bundle.putInt(Constants.CurrentHolidays, this.getHolidays());
		bundle.putInt(Constants.CurrentSick, this.getSickDays());
		bundle.putInt(Constants.CurrentZip, this.getZip());
		return bundle;
	}
	
	private int getYearlySalary()
	{
		String text = this.txtSalary.getText().toString();
		if (text == null || text == "")
			return 0;
		return (int)Double.parseDouble(text);
	}
	
	private int getVacationDays()
	{
		String text = this.txtVacation.getText().toString();
		if (text == null || text == "")
			return 0;
		return (int)Double.parseDouble(text);
	}
	
	private int getHolidays()
	{
		String text = this.txtHolidays.getText().toString();
		if (text == null || text == "")
			return 0;
		return (int)Double.parseDouble(text);
	}
	
	private int getSickDays()
	{
		String text = this.txtSick.getText().toString();
		if (text == null || text == "")
			return 0;
		return (int)Double.parseDouble(text);
	}
	
	private int getZip()
	{
		String text = this.txtZip.getText().toString();
		if (text == null || text == "")
			return 0;
		return (int)Double.parseDouble(text);
	}
}
